/****************************************Copyright (c)**************************************************
**                                         2016-03
**
**                                
**
**
**--------------File Info-------------------------------------------------------------------------------
** File name:		    ht6x1x_cmu.c
** Last modified Date:  2016-03-29
** Last Version:		v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Created by:			Evan Chen
** Created date:		2016-03-29
** Version:				v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "ht6x1x_cmu.h"
#include "ht6x1x_flash.h"

void Sys_Dealy(void)
{
	int i,j;
	unsigned int timeouts = 0x1c138;//520ms
	for(j=0;j<2;j++){
		for(i=0;i<timeouts;i++);
	}
}
void CMU_ClockSelectSet(CMU_FcpuClock_TypeDef clock, CMU_Select_TypeDef select)
{
	Sys_Dealy();
	WPREG_Disable();	
	
	if(CMU_Select_FHRC == select)
	{
		HT_CMU->HRCDIV = HRCDIV_1_1FHRC;/*default: 14M*/
		HT_CMU->CLKCTRL0 |= CLKCTRL0_HRC_EN;
		HT_CMU->SYSCLKDIV = clock;
		while (HT_CMU->CLKSTA & CLKSTA_FHRC_FLAG_MASK);
	}
	else if(CMU_Select_FPLL == select)
	{
		HT_CMU->CLKCTRL0 |= CLKCTRL0_PLL_EN;
		HT_CMU->SYSCLKDIV = clock;
		Sys_Dealy();
		while (HT_CMU->CLKSTA & CLKSTA_FPLL_FLAG_MASK);
	}
	else
	{
		HT_CMU->SYSCLKDIV = clock;
	}
		
	HT_CMU->SYSCLKCFG = select | SYSCLKCFG_WCLKEN_MASK;

	WPREG_Enable();
}

void CMU_FLRC_OutFreq(uint8_t freq)
{
	WPREG_Disable();
	HT_CMU->LRCADJ = freq;
	WPREG_Enable();
}

void CMU_FHRC_OutFreq(uint8_t freq)
{
	WPREG_Disable();
	HT_CMU->HRCADJ = freq;
	WPREG_Enable();
}

/*select/2*(div+1)*/
void CMU_ClockOutSelectSet(CMU_OutSelect_TypeDef select, uint16_t div)
{
	WPREG_Disable();

	HT_CMU->CLKOUTSEL = select;
	HT_CMU->CLKOUTDIV = div;

	WPREG_Enable();
}

void CMU_Clock0Enable(CMU_Clock0_TypeDef clock)
{
	WPREG_Disable();
	
	HT_CMU->CLKCTRL0 |= clock;

	WPREG_Enable();
}

void CMU_Clock0AndEnable(CMU_Clock0_TypeDef clock)
{
	WPREG_Disable();
	
	HT_CMU->CLKCTRL0 &= clock;

	WPREG_Enable();
}

void CMU_Clock0Disable(CMU_Clock0_TypeDef clock)
{
	WPREG_Disable();
	
	HT_CMU->CLKCTRL0 &= ~clock;

	WPREG_Enable();
}

void CMU_Clock1Enable(CMU_Clock1_TypeDef clock)
{
	WPREG_Disable();
	
	HT_CMU->CLKCTRL1 |= clock;

	WPREG_Enable();
}

void CMU_Clock1AndEnable(CMU_Clock1_TypeDef clock)
{
	WPREG_Disable();
	
	HT_CMU->CLKCTRL1 &= clock;

	WPREG_Enable();
}

void CMU_Clock1Disable(CMU_Clock1_TypeDef clock)
{
	WPREG_Disable();
	
	HT_CMU->CLKCTRL1 &= ~clock;

	WPREG_Enable();
}

void CMU_ClockAllDisable(void)
{
	WPREG_Disable();
	
	HT_CMU->CLKCTRL0 &= ~CLKCTRL0_PERIPH_BIS;
	HT_CMU->CLKCTRL1 &= ~CLKCTRL1_PERIPH_BIS;

	WPREG_Enable();
}

uint16_t CMU_ReadClock(CMU_Read_TypeDef clock)
{
	if(CMU_CLKCTRL0 == clock)
	{
		return HT_CMU->CLKCTRL0;
	}

	return HT_CMU->CLKCTRL1;
}

void CMU_WriteClock(CMU_Read_TypeDef clock, uint16_t val)
{
	WPREG_Disable();
	
	if(CMU_CLKCTRL0 == clock)
	{
		HT_CMU->CLKCTRL0 = val;
	}
	else
	{
		HT_CMU->CLKCTRL1 = val;
	}

	WPREG_Enable();
}

